<template>
  <div>
    <div class="box"></div>
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      class="login_form"
    >
      <h3 class="login_title">系统登陆</h3>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="form.password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="margin-left: 24px" @click="submit"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Mock from "mockjs";
import Cookie from "js-cookie";
import { getMenu } from "../api";
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 登录
    submit() {
      // 校验用户密码
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 校验通过
          getMenu(this.form).then(({ data }) => {
            if (data.code === 20000) {
              // 存入token到cookie中用于不同页面通信
              Cookie.set("token", data.data.token);
              // 将菜单数据存入到store中
              this.$store.commit("setMenu", data.data.menu);
              // 组装路由数据，动态注册路由
              this.$store.commit("addMenu", this.$router);
              // 跳转到首页
              this.$router.push("/home");
            } else {
              this.$message.error(data.data.message);
            }
          });
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.box {
  position: fixed;
  top: 0;
  left: 0;
  z-index: -1;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: url(../assets/images/bg.png) no-repeat center center/cover;
  filter: blur(3px);
}
.login_form {
  width: 350px;
  margin: 180px auto;
  border: 1px solid #eaeaea;
  padding: 35px 35px 15px 35px;
  box-sizing: border-box;
  border-radius: 15px;
  background-color: #fff;
  box-shadow: 0 0 25px #cac6c6;
  .login_title {
    text-align: center;
    color: #333;
    margin-bottom: 30px;
  }
  .el-input {
    width: 198px;
  }
}
</style>
